<?php



class Main_user extends Controller {



    function Main_user() {

        parent::Controller();    

        $this->load->model('w_user_mod', 'u');              

    }

    

 

    

    function index() {

        if (!$this->session->userdata('username')) {

            $data['isLogin'] = FALSE;

            redirect("/login", "location");

        } else {

            $data['isLogin'] = TRUE;

            $data['username'] = $this->session->userdata('username');

            $data['role'] = $this->session->userdata('role');

            

            $this->list_user();

        } 

    }

    



    //user

    function list_user() {

        if (!$this->session->userdata('username')) {

            $data['isLogin'] = FALSE;

            redirect("/login", "location");

        } else {

            $data['isLogin'] = TRUE;

            $data['username'] = $this->session->userdata('username');

            $data['role'] = $this->session->userdata('role');

        }  

        

        $data['additional_js'] = '

        <script type="text/javascript">
        function deleteRow(id){
        var txt = \'Anda yakin mau menghapus data ini?<input type="hidden" id="row-id" name="row-id" value="\'+ id +\'" />\';

                url = "'.base_url().'index.php/main_user/hapus_user/"+id;

             $.ajax({

               type: "POST",

               url: "'.base_url().'index.php/main_user/user_bisa_dihapus/"+id,

               success: function(msg) {

                 if (msg==\'bisa\') {

                    $.prompt(txt,{

                        buttons:{Hapus:true, Batal:false},

                        callback: function(v){

                            if(v) {

                                $("tr#row-"+id).fadeOut("slow", function() {

                                    $("tr#row-"+id).remove();

                                });

                                top.location.href=url;

                            }

                        }

                    });

                 } else {

                    alert(\'Maaf, data user tidak dapat dihapus dikarenakan sedang digunakan oleh data lain.\');

                 }

               }

            });   }  </script>    

            '."<script>
            $(document).ready(function() {
                $(\".akses\").change(function(){
                    var id = $(this).attr('id');
                    var val = $(this).val();
                    
                    $.post('".base_url()."index.php/main_user/hak_akses',{id:id, id_akses:val});  
                    
                });
            });</script>
            ";

     

          

        $data['list_data'] = $this->u->list_user();

        $data['judul'] = 'LIST DATA USER';

        $data['tampilkan_menu'] = true; 

        $data['aksi'] = 'main_user/tambah_user';    

        $this->load->view('v_user',$data); 

    }

    

    function tambah_user() {

        if (!$this->session->userdata('username')) {

            $data['isLogin'] = FALSE;

            redirect("/login", "location");

        } else {

            $data['isLogin'] = TRUE;

            $data['username'] = $this->session->userdata('username');

            $data['role'] = $this->session->userdata('role');

        }  

        

        $data['additional_js'] = '

        <script type="text/javascript">

            $(function() {

            $("#namauser").focus();

            $("#btn_kembali").click( function() {

                    window.location = \''.base_url().'index.php/main_user\';

                });

            // validate contact form on keyup and submit

            $("#tambahuser").validate({

                //set the rules for the field names

                rules: {

                    namauser: { required: true },

                    nama_lengkap: { required: true },

                    passwd: { required: true },

                    passwd2: { required: true },

                    jenis_admin: { required: true }

                },

                //set messages to appear inline

                messages: {

                    namauser: "Nama user wajib diisi",

                    nama_lengkap: "Nama lengkap wajib diisi",

                    passwd: "Password wajib diisi",

                    passwd2: "Password wajib diisi",

                    jenis_admin: "Grup user wajib diisi"

                }

            });



            $("form").submit(function() {

                if ($("#passwd").val() != $("#passwd2").val()) {

                    alert(\'Password harus sama!!!\');

                    return false;

                }

            });

        });

        </script>

';



        $data['additional_css'] = '

            <style type="text/css">

            .error {

                color: red;

                font: 8pt verdana;

                padding-left: 10px

            }

            </style>

        ';

        

        $data['list_data'] = $this->u->entry_user('simpan_user');

            

        $data['url_list'] = base_url().'index.php/main_user/list_user'; 

        $data['judul'] = 'TAMBAH USER';

       

        $this->load->view('v_user',$data); 

        

    }

    

    



    function simpan_user() {

        if (!$this->session->userdata('username')) {

            $data['isLogin'] = FALSE;

            redirect("/login", "location");

        } else {

            $data['isLogin'] = TRUE;

            $data['username'] = $this->session->userdata('username');

            $data['role'] = $this->session->userdata('role');

        }  

        



        $namauser = $this->input->post('namauser');

        $nama_lengkap = $this->input->post('nama_lengkap');

        $passwd = md5($this->input->post('passwd'));

        $id_grup = $this->input->post('jenis_admin');

        

       

        $this->u->simpan_user($namauser,$nama_lengkap,$passwd,$id_grup);                 

        

        redirect('main_user');     

         

    }

    

    function edit_user($namauser) {

        if (!$this->session->userdata('username')) {

            $data['isLogin'] = FALSE;

            redirect("/login", "location");

        } else {

            $data['isLogin'] = TRUE;

            $data['username'] = $this->session->userdata('username');

            $data['role'] = $this->session->userdata('role');

        }  

        

        $data['additional_js'] = '

        <script type="text/javascript">

            $(function() {

            $("#namauser").focus();

            $("#btn_kembali").click( function() {

                    window.location = \''.base_url().'index.php/main_user\';

                });

            // validate contact form on keyup and submit

            $("#tambahuser").validate({

                //set the rules for the field names

                rules: {

                    namauser: { required: true },

                    nama_lengkap: { required: true },

                    passwd: { required: true },

                    passwd2: { required: true },

                    jenis_admin: { required: true }

                },

                //set messages to appear inline

                messages: {

                    namauser: "Nama user wajib diisi",

                    nama_lengkap: "Nama lengkap wajib diisi",

                    passwd: "Password wajib diisi",

                    passwd2: "Password wajib diisi",

                    jenis_admin: "Grup user wajib diisi"

                }

            });



            $("form").submit(function() {

                if ($("#passwd").val() != $("#passwd2").val()) {

                    alert(\'Password harus sama!!!\');

                    return false;

                }

            });

        });

        </script>

';



        $this->db->where(array('namauser' => $namauser));

        $q = $this->db->get('user');

        if ($q->num_rows() > 0) {

            $user = $q->row();

            $namauser        = $user->namauser;

            $nama_lengkap    = $user->nama_asli;

            $id_grup         = $user->id_grup;

        }

        

        $data['list_data'] = $this->u->entry_user('update_user',$namauser,$nama_lengkap,'',$id_grup);

        $data['judul'] = 'EDIT USER';

            

        $this->load->view('v_user',$data); 

       

    }

    

    function update_user() {

        if (!$this->session->userdata('username')) {

            $data['isLogin'] = FALSE;

            redirect("/login", "location");

        } else {

            $data['isLogin'] = TRUE;

            $data['username'] = $this->session->userdata('username');

            $data['role'] = $this->session->userdata('role');

        }  

     

        $namauser = $this->input->post('namauser');

        $nama_lengkap = $this->input->post('nama_lengkap');

        if ($this->input->post('passwd')) {

            $passwd = md5($this->input->post('passwd'));

        } else {

            $passwd = '';

        }

        $id_grup = $this->input->post('jenis_admin');

        

        $this->u->simpan_user($namauser,$nama_lengkap,$passwd,$id_grup,TRUE);                 

        

        redirect('main_user');      

    }

    

    function hapus_user($namauser) {

        if (!$this->session->userdata('username')) {

            $data['isLogin'] = FALSE;

            redirect("/login", "location");

        } else {

            $data['isLogin'] = TRUE;

            $data['username'] = $this->session->userdata('username');

            $data['role'] = $this->session->userdata('role');

        }

        

        $this->db->where('namauser', $namauser);

        $this->db->delete('user');

        redirect('main_user');

    }

    

    //grup    

    function tambah_grup() {

        if (!$this->session->userdata('username')) {

            $data['isLogin'] = FALSE;

            redirect("/login", "location");

        } else {

            $data['isLogin'] = TRUE;

            $data['username'] = $this->session->userdata('username');

            $data['role'] = $this->session->userdata('role');

        }  

        

         $data['additional_js'] = '

        <script type="text/javascript">

            $(function() {

            

            $("#btn_kembali").click( function() {

                    window.location = \''.base_url().'index.php/main_user/list_grup\';

                });

            // validate contact form on keyup and submit

            $("#grup").validate({

                //set the rules for the field names

                rules: {

                    nama: { required: true },

                    

                   

                },

                //set messages to appear inline

                messages: {

                    nama: "Nama Grup wajib diisi",

                    

                    

                }

            });



      

        });

        </script>

        ';

        

        $action = base_url().'index.php/main_user/simpan_grup';

        $data['list_data'] = $this->u->entry_grup($action); 

        $data['judul'] = 'TAMBAH GRUP';

        $data['url_list'] = base_url().'index.php/main_user/list_grup';                

        $this->load->view('v_user',$data);    



       

    }

    

    

     function list_grup() {

       if (!$this->session->userdata('username')) {

            $data['isLogin'] = FALSE;

            redirect("/login", "location");

        } else {

            $data['isLogin'] = TRUE;

            $data['username'] = $this->session->userdata('username');

            $data['role'] = $this->session->userdata('role');

        }  

        

        $data['additional_js'] = '

        <script type="text/javascript">

            function deleteRow(id) {

                var txt = \'Anda yakin mau menghapus data ini?<input type="hidden" id="row-id" name="row-id" value="\'+ id +\'" />\';

                    url = "'.base_url().'index.php/main_user/hapus_user/"+id;

                 

                        $.prompt(txt,{

                            buttons:{Hapus:true, Batal:false},

                            callback: function(v){

                                if(v) {

                                    $("tr#row-"+id).fadeOut("slow", function() {

                                        $("tr#row-"+id).remove();

                                    });

                                    top.location.href=url;

                                }

                            }

                        });

                     



            }     </script>    

            ';

        $data['additional_js'] = '

        <script type="text/javascript">        

        function deleteRow(id) {

            var txt = \'Anda yakin mau menghapus data ini?<input type="hidden" id="row-id" name="row-id" value="\'+ id +\'" />\';

                url = "'.base_url().'index.php/main_user/hapus_grup/"+id;

             $.ajax({

               type: "POST",

               url: "'.base_url().'index.php/main_user/grup_bisa_dihapus/"+id,

               success: function(msg) {

                 if (msg==\'bisa\') {

                    $.prompt(txt,{

                        buttons:{Hapus:true, Batal:false},

                        callback: function(v){

                            if(v) {

                                $("tr#row-"+id).fadeOut("slow", function() {

                                    $("tr#row-"+id).remove();

                                });

                                top.location.href=url;

                            }

                        }

                    });

                 } else {

                    alert(\'Maaf, data grup tidak dapat dihapus dikarenakan sedang digunakan oleh data lain.\');

                 }

               }

            });

        }

         </script> 

        ';



        $data['list_data'] = $this->u->list_grup();

        $data['judul'] = 'LIST DATA GRUP';

        $data['tampilkan_menu'] = true; 

        $data['aksi'] = 'tambah_grup'; 

        $this->load->view('v_user',$data); 

        //echo  $this->l->list_grup();

       

    }

    

    function simpan_grup() {

       if (!$this->session->userdata('username')) {

            $data['isLogin'] = FALSE;

            redirect("/login", "location");

        } else {

            $data['isLogin'] = TRUE;

            $data['username'] = $this->session->userdata('username');

            $data['role'] = $this->session->userdata('role');

        }   

        

        $nama = $this->input->post('nama');

        

        $this->u->simpan_grup('grup', $nama);                    

    

        redirect('main_user/list_grup');        

    

    }

    

    

    function edit_grup($id) {

        if (!$this->session->userdata('username')) {

            $data['isLogin'] = FALSE;

            redirect("/login", "location");

        } else {

            $data['isLogin'] = TRUE;

            $data['username'] = $this->session->userdata('username');

            $data['role'] = $this->session->userdata('role');

        }   

        

        $data['additional_js'] = '

        <script type="text/javascript">

            $(function() {

            

            $("#btn_kembali").click( function() {

                    window.location = \''.base_url().'index.php/main_user/list_grup\';

                });

            // validate contact form on keyup and submit

            $("#grup").validate({

                //set the rules for the field names

                rules: {

                    nama: { required: true },

                    

                   

                },

                //set messages to appear inline

                messages: {

                    nama: "Nama Grup wajib diisi",

                    

                    

                }

            });



      

        });

        </script>

        ';

        

        $this->db->where(array('idspg_grup' => $id));

        $q = $this->db->get('grup');

        if ($q->num_rows() > 0) {

            $grup       = $q->row();

            $nama       = $grup->nama;

            $id_grup    = $grup->idspg_grup;

        }

        

        

        $action = base_url().'index.php/main_user/update_grup';

        $data['list_data'] = $this->u->entry_grup($action,$nama,$id_grup);

        $data['judul'] = 'EDIT GRUP';

        $data['url_list'] = base_url().'index.php/main_user/list_grup';

        

        $this->load->view('v_user',$data); 

        

    }

    

    function update_grup() {

        if (!$this->session->userdata('username')) {

            $data['isLogin'] = FALSE;

            redirect("/login", "location");

        } else {

            $data['isLogin'] = TRUE;

            $data['username'] = $this->session->userdata('username');

            $data['role'] = $this->session->userdata('role');

        }   

     

        $nama = $this->input->post('nama');

        $id = $this->input->post('id');

        $this->u->simpan_grup('grup', $nama, $id, TRUE);                 

        

        redirect('main_user/list_grup');         

    }

    

    function hapus_grup($id) {

        if (!$this->session->userdata('username')) {

            $data['isLogin'] = FALSE;

            redirect("/login", "location");

        } else {

            $data['isLogin'] = TRUE;

            $data['username'] = $this->session->userdata('username');

            $data['role'] = $this->session->userdata('role');

        } 

        

        $this->db->where('idspg_grup', $id);

        $this->db->delete('grup');

        redirect('main_user/list_grup');     

        

    }

    

    function grup_bisa_dihapus($id) {

        $this->db->where('id_grup', $id);

        $q = $this->db->get('user');

        if ($q->num_rows() > 0) {

            echo 'tidak';

        } else {

            echo 'bisa';

        }

    }



    function user_bisa_dihapus($namauser) {
       if (!$this->session->userdata('username')) {

            $data['isLogin'] = FALSE;

            redirect("/login", "location");

        } 
       echo "bisa";

    }


    function hak_akses() {
        if (!$this->session->userdata('username')) {

            $data['isLogin'] = FALSE;

            redirect("/login", "location");

        }
       
       $namauser = base64_decode( $this->input->post('id') );
       $id_akses = $this->input->post('id_akses');
             
        $this->db->where('namauser', $namauser);

        $this->db->update('user',array('id_akses' => $id_akses)); 

    }

}



/* End of file main_admin.php */

/* Location: ./system/application/controllers/main_user.php */